//
//  weightLogViewController.m
//  Wtracker
//
//  Created by Anna on 7/5/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "weightLogViewController.h"
#import "WtrackerAppDelegate.h"

#define UOMTITLE @"Unit of Measure"

@implementation weightLogViewController

@synthesize weights, unitArray, myDict, theWeight, theDate;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	NSLog(@"in wlvc");
	
	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	weights = appDelegate.weights;
	unitArray = appDelegate.unitArray;
	myDict = appDelegate.myDict;
	
	self.navigationItem.title = @"Weight Log";
	self.navigationItem.rightBarButtonItem = self.editButtonItem;

	NSLog(@"the weights: %@", weights);
	
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [weights count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	NSArray *tmp1 = [[NSArray alloc] initWithArray:[weights objectAtIndex:indexPath.row]];
	NSString *myUOMStr = [myDict objectForKey:UOMTITLE];
	NSNumber *UOMidx = [NSNumber numberWithInt:[unitArray indexOfObject:myUOMStr]];
	
	// the weight in each record in DB is stored one column up from where it is in UnitArray
	float weight = [[tmp1 objectAtIndex:([UOMidx
										  intValue]+1)] doubleValue];
	
	NSString *weightStr = [NSString stringWithFormat:@"%.1f", weight];
	
	NSString *thedate = [tmp1 objectAtIndex:3];
	NSString *rowStr = [NSString stringWithFormat:@"%@,  %@ %@", thedate, weightStr, myUOMStr ];

	cell.textLabel.text = rowStr;

    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath  *)indexPath {
	
	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	
    if (editingStyle == UITableViewCellEditingStyleDelete) {

//		[weights removeObjectAtIndex:indexPath.row];
		BOOL success;
		success = [appDelegate removeFromList:indexPath.row];
		if(!success){
			NSString *theMessage = @"Failed to delete record.";
			[self showAlert:theMessage];
		}
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
	
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
			// redirect to entry page
    }    	
}


 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath  *)fromIndexPath toIndexPath:(NSIndexPath  *)toIndexPath {
//	 WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
//	 [appDelegate moveFromOriginal:fromIndexPath.row toNew:toIndexPath.row];
 
 }

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}





#pragma mark -  
#pragma mark custom methods

-(void)showAlert:(NSString *)theMessage{
	UIAlertView *infoAlert = [[[UIAlertView alloc] initWithTitle:@"Error" message:theMessage delegate:self cancelButtonTitle:@"OK" 
											   otherButtonTitles:nil] autorelease];
	[infoAlert show];
	
	
}





#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	//[weights release];
//	[myDict release];
//	[unitArray release];
    [super dealloc];
}


@end

